package edu.intprog.egreeting;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.intprog.configuration.ConfigurationManager;
import edu.intprog.egreeting.basic.EgreetingBaseServlet;
import edu.intprog.egreeting.beans.Greeting;

/**
 * Servlet implementation class for Servlet: SearchResultsServlet
 *
 */
public class SearchResultsServlet extends EgreetingBaseServlet implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public SearchResultsServlet() {
		super();
	}   	

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("select")!=null) {
			//"select" was pressed - update the email and name of the chosen recipient
			String recipientEmail = request.getParameter("selectedResult");
			Greeting greeting = (Greeting) request.getSession().getAttribute("greetingTemplate");
			Properties results = (Properties) request.getSession().getAttribute("searchResults");
			greeting.set_recipientEmail(recipientEmail);
			greeting.set_recipientName(results.getProperty(recipientEmail));
		}
		
		//add the greeting required data into the request
		ConfigurationManager.fillRequestWithGreetingEssentials(request);
		
		request.getSession().removeAttribute("searchResults");

		getServletContext().getRequestDispatcher("/greetingTemplate.jsp").forward(request, response);
		return;
	}   	  	    
}